# -*- coding: utf-8 -*-
# Powered By: ECHO
# @Time : 2020/8/22 14:59
import cv2
from selenium import webdriver
import time

# driver = webdriver.Chrome()
# driver.implicitly_wait(10)
# driver.maximize_window()
# driver.get('https://i.qq.com/')
# time.sleep(5)
# driver.switch_to.frame(driver.find_elements_by_xpath('//*[@id="login_frame"]'))
# time.sleep(5)
# driver.find_elements_by_xpath('//*[@id="switcher_plogin"]').click()
# driver.find_elements_by_xpath('//*[@id="u"]').send_keys('495123451')
# driver.find_elements_by_xpath('//*[@id="p"]').send_keys('dddjkkj')
# driver.find_elements_by_xpath('//*[@id="login_button"]').click()
from selenium.webdriver import ActionChains

from slide.getimg import get_img, FindPic

driver = webdriver.Chrome()
driver.implicitly_wait(10)
driver.maximize_window()
driver.get('https://i.qq.com/')

driver.switch_to.frame(driver.find_element_by_xpath('//*[@id="login_frame"]'))
driver.find_element_by_xpath('//*[@id="switcher_plogin"]').click()
driver.find_element_by_xpath('//*[@id="u"]').send_keys('213123123')
driver.find_element_by_xpath('//*[@id="p"]').send_keys('123213')
driver.find_element_by_xpath('//*[@id="login_button"]').click()

time.sleep(3)
driver.switch_to.frame(driver.find_element_by_xpath('//*[@id="tcaptcha_iframe"]'))
ele1 = driver.find_element_by_xpath('//*[@id="slideBg"]')
ele2= driver.find_element_by_xpath('//*[@id="slideBlock"]')

#下载图片
get_img(ele1.get_attribute("src"),'./lib/target.png')
get_img(ele2.get_attribute("src"),'./lib/template.png')
#获取偏移量
x = FindPic()
print(x)
#计算图片缩放
w1 = ele1.size['width'] #图片在网页上的宽
img = cv2.imread('./lib/target.png')
w2 = img.shape[1] #原始图片宽度

x = int(x*w1/w2)-22 #因为滑块不是从背景图的最左边开始的
print(x)
#滑动
action = ActionChains(driver)
action.click_and_hold(ele2)#按住元素
action.move_by_offset(x,0).perform() #滑动x和y
action.release().perform() #释放鼠标
